using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace MaterialUI
{
	[ExecuteInEditMode]
	[RequireComponent(typeof(Button))]
	[RequireComponent(typeof(RectTransform))]
	[RequireComponent(typeof(CanvasGroup))]
	[AddComponentMenu("MaterialUI/Material Button", 100)]
	public class MaterialButton : UIBehaviour, ILayoutGroup, ILayoutElement, ILayoutSelfController, ILayoutController
	{
		private const string pathToCirclePrefab = "Assets/MaterialUI/Prefabs/Components/Buttons/Floating Action Button.prefab";

		private const string pathToRectPrefab = "Assets/MaterialUI/Prefabs/Components/Buttons/Button.prefab";

		[SerializeField]
		private RectTransform m_RectTransform;

		[SerializeField]
		private RectTransform m_ContentRectTransform;

		[SerializeField]
		private Button m_ButtonObject;

		[SerializeField]
		private Graphic m_BackgroundImage;

		[SerializeField]
		private Text m_Text;

		[SerializeField]
		private Graphic m_Icon;

		[SerializeField]
		private MaterialRipple m_MaterialRipple;

		[SerializeField]
		private MaterialShadow m_MaterialShadow;

		[SerializeField]
		private CanvasGroup m_CanvasGroup;

		[SerializeField]
		private CanvasGroup m_ShadowsCanvasGroup;

		[SerializeField]
		private bool m_Interactable = true;

		[SerializeField]
		private Vector2 m_ContentPadding = new Vector2(30f, 18f);

		[SerializeField]
		private Vector2 m_ContentSize;

		[SerializeField]
		private Vector2 m_Size;

		[SerializeField]
		private bool m_FitWidthToContent;

		[SerializeField]
		private bool m_FitHeightToContent;

		[SerializeField]
		private bool m_IsCircularButton;

		[SerializeField]
		private bool m_IsRaisedButton;

		private DrivenRectTransformTracker m_Tracker = default(DrivenRectTransformTracker);

		public RectTransform rectTransform
		{
			get
			{
				if (m_RectTransform == null)
				{
					m_RectTransform = (RectTransform)base.transform;
				}
				return m_RectTransform;
			}
		}

		public RectTransform contentRectTransform
		{
			get
			{
				return m_ContentRectTransform;
			}
			set
			{
				m_ContentRectTransform = value;
				SetLayoutDirty();
			}
		}

		public Button buttonObject
		{
			get
			{
				if (m_ButtonObject == null)
				{
					m_ButtonObject = base.gameObject.GetAddComponent<Button>();
				}
				return m_ButtonObject;
			}
		}

		public Graphic backgroundImage
		{
			get
			{
				return m_BackgroundImage;
			}
			set
			{
				m_BackgroundImage = value;
			}
		}

		public Text text
		{
			get
			{
				return m_Text;
			}
			set
			{
				m_Text = value;
				SetLayoutDirty();
			}
		}

		public Graphic icon
		{
			get
			{
				return m_Icon;
			}
			set
			{
				m_Icon = value;
				SetLayoutDirty();
			}
		}

		public MaterialRipple materialRipple
		{
			get
			{
				if (m_MaterialRipple == null)
				{
					m_MaterialRipple = GetComponent<MaterialRipple>();
				}
				return m_MaterialRipple;
			}
		}

		public MaterialShadow materialShadow
		{
			get
			{
				if (m_MaterialShadow == null)
				{
					m_MaterialShadow = GetComponent<MaterialShadow>();
				}
				return m_MaterialShadow;
			}
		}

		public CanvasGroup canvasGroup
		{
			get
			{
				if (m_CanvasGroup == null)
				{
					m_CanvasGroup = base.gameObject.GetAddComponent<CanvasGroup>();
				}
				return m_CanvasGroup;
			}
		}

		public CanvasGroup shadowsCanvasGroup
		{
			get
			{
				return m_ShadowsCanvasGroup;
			}
			set
			{
				m_ShadowsCanvasGroup = value;
			}
		}

		public bool interactable
		{
			get
			{
				return m_Interactable;
			}
			set
			{
				m_Interactable = value;
				m_ButtonObject.interactable = m_Interactable;
				canvasGroup.alpha = ((!m_Interactable) ? 0.5f : 1f);
				canvasGroup.blocksRaycasts = m_Interactable;
				if ((bool)shadowsCanvasGroup)
				{
					shadowsCanvasGroup.alpha = ((!m_Interactable) ? 0f : 1f);
				}
			}
		}

		public Vector2 contentPadding
		{
			get
			{
				return m_ContentPadding;
			}
			set
			{
				m_ContentPadding = value;
				SetLayoutDirty();
			}
		}

		public Vector2 contentSize => m_ContentSize;

		public Vector2 size => m_Size;

		public bool fitWidthToContent
		{
			get
			{
				return m_FitWidthToContent;
			}
			set
			{
				m_FitWidthToContent = value;
				m_Tracker.Clear();
				SetLayoutDirty();
			}
		}

		public bool fitHeightToContent
		{
			get
			{
				return m_FitHeightToContent;
			}
			set
			{
				m_FitHeightToContent = value;
				m_Tracker.Clear();
				SetLayoutDirty();
			}
		}

		public bool isCircularButton
		{
			get
			{
				return m_IsCircularButton;
			}
			set
			{
				m_IsCircularButton = value;
			}
		}

		public bool isRaisedButton
		{
			get
			{
				return m_IsRaisedButton;
			}
			set
			{
				m_IsRaisedButton = value;
			}
		}

		public string textText
		{
			get
			{
				return m_Text.text;
			}
			set
			{
				m_Text.text = value;
			}
		}

		public Color textColor
		{
			get
			{
				return m_Text.color;
			}
			set
			{
				m_Text.color = value;
			}
		}

		public VectorImageData iconVectorImageData
		{
			get
			{
				return m_Icon.GetVectorImage();
			}
			set
			{
				m_Icon.SetImage(value);
			}
		}

		public Sprite iconSprite
		{
			get
			{
				return m_Icon.GetSpriteImage();
			}
			set
			{
				m_Icon.SetImage(value);
			}
		}

		public Color iconColor
		{
			get
			{
				return m_Icon.color;
			}
			set
			{
				m_Icon.color = value;
			}
		}

		public VectorImageData backgroundVectorImageData
		{
			get
			{
				return m_BackgroundImage.GetVectorImage();
			}
			set
			{
				m_BackgroundImage.SetImage(value);
			}
		}

		public Sprite backgroundSprite
		{
			get
			{
				return m_BackgroundImage.GetSpriteImage();
			}
			set
			{
				m_BackgroundImage.SetImage(value);
			}
		}

		public Color backgroundColor
		{
			get
			{
				return m_BackgroundImage.color;
			}
			set
			{
				m_BackgroundImage.color = value;
			}
		}

		public float minWidth => (!base.enabled) ? 0f : m_Size.x;

		public float preferredWidth => minWidth;

		public float flexibleWidth => -1f;

		public float minHeight => (!base.enabled) ? 0f : m_Size.y;

		public float preferredHeight => minHeight;

		public float flexibleHeight => -1f;

		public int layoutPriority => 1;

		protected override void OnEnable()
		{
			SetLayoutDirty();
		}

		protected override void OnDisable()
		{
			m_Tracker.Clear();
		}

		public void SetButtonBackgroundColor(Color color, bool animate = true)
		{
			if (!(m_BackgroundImage == null))
			{
				if (m_MaterialRipple != null)
				{
					m_MaterialRipple.SetGraphicColor(color, animate);
				}
				else if (animate && Application.isPlaying)
				{
					TweenManager.TweenColor(delegate(Color color1)
					{
						m_BackgroundImage.color = color1;
					}, m_BackgroundImage.color, color, 0.5f);
				}
				else
				{
					m_BackgroundImage.color = color;
				}
			}
		}

		public void RefreshRippleMatchColor()
		{
			if (m_MaterialRipple != null)
			{
				m_MaterialRipple.RefreshGraphicMatchColor();
			}
		}

		public void Convert(bool noExitGUI = false)
		{
		}

		public void ClearTracker()
		{
			m_Tracker.Clear();
		}

		protected override void OnRectTransformDimensionsChange()
		{
			SetLayoutDirty();
		}

		protected override void OnCanvasGroupChanged()
		{
			SetLayoutDirty();
		}

		protected override void OnDidApplyAnimationProperties()
		{
			SetLayoutDirty();
		}

		public void SetLayoutDirty()
		{
			LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
		}

		public void SetLayoutHorizontal()
		{
			if (m_FitWidthToContent && !(m_ContentRectTransform == null))
			{
				m_Tracker.Add(this, rectTransform, DrivenTransformProperties.SizeDeltaX);
				rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, m_Size.x);
				m_Tracker.Add(this, m_ContentRectTransform, DrivenTransformProperties.SizeDeltaX);
				m_ContentRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, m_ContentSize.x);
			}
		}

		public void SetLayoutVertical()
		{
			if (m_FitHeightToContent && !(m_ContentRectTransform == null))
			{
				m_Tracker.Add(this, rectTransform, DrivenTransformProperties.SizeDeltaY);
				rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, m_Size.y);
				m_Tracker.Add(this, m_ContentRectTransform, DrivenTransformProperties.SizeDeltaY);
				m_ContentRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, m_ContentSize.y);
			}
		}

		public void CalculateLayoutInputHorizontal()
		{
			if (m_FitWidthToContent)
			{
				if (!(m_ContentRectTransform == null))
				{
					m_ContentSize.x = LayoutUtility.GetPreferredWidth(m_ContentRectTransform);
					m_Size.x = m_ContentSize.x + m_ContentPadding.x;
				}
			}
			else
			{
				m_Size.x = -1f;
			}
		}

		public void CalculateLayoutInputVertical()
		{
			if (m_FitHeightToContent)
			{
				if (!(m_ContentRectTransform == null))
				{
					m_ContentSize.y = LayoutUtility.GetPreferredHeight(m_ContentRectTransform);
					m_Size.y = m_ContentSize.y + m_ContentPadding.y;
				}
			}
			else
			{
				m_Size.y = -1f;
			}
		}
	}
}
